#include "KeyTimer.h"

unsigned char KeyNumber;

//主函数调用的按键函数
unsigned char GetKey_Num(void)
{
	unsigned char t;
	t = KeyNumber;
	KeyNumber = 0;
	return t;
}//KeyNumber每次清零，还想要得到KeyNumber的数值的返回值，所以新建一个变量t

//----------------------------------------------------------------
//内部调用的实时扫描按键函数
unsigned char GetKeyNumber(void)
{
	unsigned char Key;	
	if(P30 == 0) Key = 7;
	if(P31 == 0) Key = 6;
	if(P32 == 0) Key = 5;
	if(P33 == 0) Key = 4;
	return Key;
}

//----------------------------------------------------------------
//定时器中断里10ms引用一次
void KeyScan(void)
{
	static unsigned char LastKey, NowKey;
	static unsigned char K7Count, K6Count, K5Count, K4Count;	//长按支持2.5s
		
	LastKey = NowKey;		//10ms前的
	NowKey  = GetKeyNumber();	//现在的
	
	if (LastKey == 7 && NowKey == 7) K7Count++;
	if (LastKey == 6 && NowKey == 6) K6Count++;
	if (LastKey == 5 && NowKey == 5) K5Count++;
	if (LastKey == 4 && NowKey == 4) K4Count++;
	
	//短按：250ms~1S     长按：1S以上,支持连续长按
	if (K7Count >= 25 && K7Count <= 100 && NowKey == 0) {KeyNumber = 7; K7Count = 0;}
	else if (K7Count > 100) {KeyNumber = 77; K7Count = 0;}
	
	if (K6Count >= 25 && K6Count <= 100 && NowKey == 0) {KeyNumber = 6; K6Count = 0;}
	else if (K6Count > 100) {KeyNumber = 66; K6Count = 0;}
	
	if (K5Count >= 25 && K5Count <= 100 && NowKey == 0) {KeyNumber = 5; K5Count = 0;}
	else if (K5Count > 100) {KeyNumber = 55; K5Count = 0;}
	
	if (K4Count >= 25 && K4Count <= 100 && NowKey == 0) {KeyNumber = 4; K4Count = 0;}
	else if (K4Count > 100) {KeyNumber = 44; K4Count = 0;}
}